import { observable, action } from 'mobx';
import { WeaForm } from 'comsMobx';
import { WeaTools,WeaLocaleProvider } from 'ecCom';
import * as API from '../apis/batchToDoc';
const getLabel = WeaLocaleProvider.getLabel;
const Base64 = WeaTools.Base64;

export class WFBatchToDocIndexStore {
    @observable loading = false;
    title;
    urlParams = {};
    fixedCondition = [];
    @observable fixedForm = new WeaForm();
    @observable hasRight=false;
    constructor(){
        this.setUrlParams = this.setUrlParams.bind(this);
        this.loadCondition = this.loadCondition.bind(this);
        this.appendFixedFormFields = this.appendFixedFormFields.bind(this);
        this.resetCondition = this.resetCondition.bind(this);
    }

    @action
    setUrlParams(params={}){
        this.urlParams = params;
        this.title = getLabel(22231,"流程存为文档");
        if(this.urlParams.tabTitle){
            this.title = Base64.decode(decodeURIComponent(this.urlParams.tabTitle));
        }
    }

    loadCondition(params={}){
        this.resetCondition(true);
        this.loading = true;
        API.getCondition({...params, ...this.urlParams}).then(datas =>{
            this.loading = false;
            this.fixedCondition = datas.condition;
            this.hasRight=datas.hasRight;
            if(datas.hasRight)
            this.fixedForm.initFormFields(datas.condition);
        });
    }

    appendFixedFormFields(value){
        this.fixedForm.updateFields(value, false);
    }

    resetCondition(reset = false){
        if(reset){
            this.fixedForm = new WeaForm();
            this.fixedCondition = [];
        }else{
            this.fixedForm && this.fixedForm.resetConditionValue();
        }
    }

}